﻿using System.Web.Mvc;
using Longtop.Web.WebSite.Shared;
using Castle.ActiveRecord.Queries;
using Castle.ActiveRecord;
using System.Linq;
using NHibernate.Criterion;
namespace Longtop.Web.Helpers {

    public static partial class MikePagerExtensions {
        public static string Pager<T>(this HtmlHelper html, IPagedModel<T> pageModel) {
            if (pageModel.PagedList.TotalPage < 2) return string.Empty;
            string actioinName = html.ViewContext.RouteData.GetRequiredString("action");
            pageModel.PagedList.PageSize = pageModel.PageSize;
            return html.MikePager(pageModel.PagedList);
        }
        public static PagedList<T> PagedFindAll<T>(this IPagedModel<T> model, int pageIndex, Order[] orders, params ICriterion[] criteria) {
            var list = ActiveRecordBase<T>.SlicedFindAll((pageIndex - 1) * model.PageSize, model.PageSize, orders, criteria);
            var pagedlist = new PagedList<T>(list.ToList(), pageIndex, model.PageSize);
            CountQuery q = new CountQuery(typeof(T), criteria);
            pagedlist.TotalCount = (int)ActiveRecordBase<T>.ExecuteQuery(q);
            return pagedlist;
        }

    }
}

